<?php


namespace App\Http\Controllers\Api\Passenger;


use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
use App\Services\UsersService;
use App\Transforms\UserTransformer;
use Illuminate\Http\Request;

class UserController extends Controller
{

    /**
     * 乘客列表
     * @param Request $request
     * @param UserRepository $repository
     * @return UserController|\Illuminate\Http\JsonResponse
     * @Author 姿势就是力量
     */
    public function getPageList(Request $request, UserRepository $repository)
    {

        $companyId = (int)$request->get('company_id', '');   //公司
        $pageSize = (int)$request->get('page_size', 10);
        $nick_name = (string)$request->get('nick_name', '');     //昵称
        $phone = (string)$request->get('phone', '');       //手机号
        $sex = (int)$request->get('sex', '');           //性别
        $name = (string)$request->get('name', '');           //公司
        $date = (string)$request->get('created_at','');      //日期
        $balance = (string)$request->get('balance','');      //余额
        $linkman = (string)$request->get('linkman','');      //紧急联系人
        $linkphone = (string)$request->get('linkphone','');      //紧急联系人电话
        $columns =  ['id', 'nick_name','balance','created_at', 'phone', 'sex', 'balance', 'linkman', 'linkphone', 'company_id'];
        $list = $repository->getPageList($companyId,$sex,$pageSize,$columns,$nick_name,$phone,$name,$date,$balance,$linkman,$linkphone);
        $list['data'] = (new UserTransformer())->transformList($list['data']);
        return $this->sendJson($list);
    }


    /**
     * 乘客详情
     * @param Request $request
     * @param UserRepository $repository
     * @return UserController|\Illuminate\Http\JsonResponse
     * @Author 姿势就是力量
     */
    public function getFirst(Request $request, UserRepository $repository)
    {
        $id = (int)$request->get('id', '');
        $user = $repository->find($id);
        $user = (new UserTransformer())->transform($user);

        return $this->sendJson($user);
    }


    /**
     * 保存用户信息
     * @param Request $requests
     * @param UsersService $service
     * @return UserController|\Illuminate\Http\JsonResponse
     * @Author 姿势就是力量
     */
    public function save(Request $requests, UsersService $service)
    {
        $user = $service->saveUser($requests->post());
        if (!$user) return $this->sendError($service->getError());

        return $this->sendSuccess('保存成功');
    }


    /**
     *
     * 删除乘客
     * @param Request $request
     * @param UserRepository $repository
     * @return UserController|\Illuminate\Http\JsonResponse
     * @Author 姿势就是力量
     */
    public function delete(Request $request, UserRepository $repository)
    {
        $res = $repository->destroy((int)$request->post('id', ''));
        if (!$res) return $this->sendError('删除失败');

        return $this->sendSuccess('删除成功');
    }

}
